/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/


#include "MkTcp/MkTcpClient.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"
#include "MkUtil/MkThread.h"
#include <signal.h>

CMkTimer g_Timer;
BOOL g_bRun = FALSE;
int main(int argc, char ** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
#endif
    MkLogInitLevel("TestTcpServer", TO_SCREEN);
    MkLogChangeLevel(LOG_DEBUG);
    g_bRun = TRUE;
    CMkEventPool Pool;
    Pool.Start(1);
    for (Uint32 i = 0; i < 1; i++) {
        CMkTcpClient *pTcpClient = new CMkTcpClient(Pool.GetFreeEvent(), &g_Timer, [](CMkTcpClient* pClient) {
            MkDelete(pClient);
        });
        pTcpClient->ConnectServer("192.168.0.24", 80, [pTcpClient]() {
            Uint8 pbuf[Len2K * 20] = { 0 };
            Uint32 nLen = Len2K * 20;
            pTcpClient->Send(pbuf, nLen);
        });
    }

    g_Timer.Dispatch();

    signal(SIGINT, [](int sig) {
        if (SIGINT == sig) {
            g_bRun = FALSE;
            g_Timer.LoopBreak();
        }
    });// set exit  signal

    ////pClient = new CMkTcpClient;
    //pClient->Create();
    //pEvent->Start();
    //pEvent->AddEventer(pClient, CMkEvent::EventRead|CMkEvent::EventError);
    //pClient->Connect("127.0.0.1", 6129);
    //pThread = new CMkThread;
    //pThread->Start(DoThread, nullptr);
    while (1) {
        CMkThread::ThreadSleep(1);
    }
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}